package demo6;

import java.lang.reflect.Constructor;

public class Demo {
    public static void main(String[] args) throws Exception {
//        Singleton s1 = new Singleton();
//        Singleton s2 = new Singleton();

        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();

        System.out.println(s1);
        System.out.println(s2);

        //暴力反射可以突破限制，但是在实际开发中应当谨慎
        Class cls = Class.forName("demo6.Singleton");
        Constructor c = cls.getDeclaredConstructor();//获取无参构造器
        c.setAccessible(true);//强行开启访问权限
        Singleton c3 = (Singleton) c.newInstance();
        Singleton c4 = (Singleton) c.newInstance();
        System.out.println(c3);
        System.out.println(c4);
    }
}








